
package net.swa.system.web.action;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import net.swa.system.beans.entity.Dict;
import net.swa.system.service.ICommonService;
import net.swa.system.service.IDictService;
import net.swa.system.util.annotation.Log;
import net.swa.util.JsonResult;
/**
 * 字典action
 * 
 * 
 */
public class DictAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -985277678565361630L;

    private Long[] dicIds;

    private Long[] dicNums;
    
    private Long[] ids;

    private ICommonService commonService;

    private IDictService dictService;

    private Dict dict;

    /**传入的dict title**/
    private String type;

    /** 字典类型列表 **/
    private List<Dict> typeList;

    /**
     * 列表页面
     * 
     * @return
     */
    @Log(description = "字典列表")
    public String listPage() throws Exception
    {
        typeList = dictService.getDictType();
        return "list";
    }

    /**
    * 列表页面
    * 
    * @return
    */
    @Log(description = "查询品牌型号")
    public String listPage2() throws Exception
    {
        typeList = dictService.getDictType2();
        return "list2";
    }

    /**
     * 编辑、新建操作
     * 
     * @return
     * @throws Exception
     */
    @Log(description = "编辑字典")
    public String edit() throws Exception
    {
        if (dict == null)
        {
            dict = new Dict();
        }
        else
        {
            dict = commonService.commonFind(Dict.class, dict.getId());
        }
        return "edit";
    }

    /**
     * 编辑、新建操作
     * 
     * @return
     * @throws Exception
     */
    @Log(description = "编辑品牌类型")
    public String edit2() throws Exception
    {
        if (dict == null)
        {
            dict = new Dict();
            if (!StringUtils.isBlank(type))
            {
                dict.setTitle(type);
            }
        }
        else
        {
            dict = commonService.commonFind(Dict.class, dict.getId());
        }
        return "edit2";
    }

    /**
     * 保存操作
     * 
     * @return
     * @throws Exception
     */
    public String save() throws Exception
    {
        JsonResult<Dict> json = new JsonResult<Dict>();
        if (dict.getId() == 0)
        {
            commonService.commonAdd(dict);
        }
        else
        {
            commonService.commonUpdate(dict);
        }
        outJson(json);
        return null;
    }

    /**
     * 保存操作
     * 
     * @return
     * @throws Exception
     */
    public String save2() throws Exception
    {
        JsonResult<Dict> json = new JsonResult<Dict>();
        if (!StringUtils.isBlank(dict.getValue()))
        {
            if ("不限".equals(dict.getValue().trim()))
            {
                dict.setKey("");
            }
            else
            {
                dict.setKey(dict.getValue());
            }
        }
        if (dict.getId() == 0)
        {
            commonService.commonAdd(dict);
        }
        else
        {
            commonService.commonUpdate(dict);
        }
        outJson(json);
        return null;
    }

    /**
     * 保存排序
     * 
     * @return
     * @throws Exception
     */
    public String savesort() throws Exception
    {
        dictService.updateDicNum(dicIds, dicNums);
        JsonResult<Dict> json = new JsonResult<Dict>();
        outJson(json);
        return null;
    }
    
    public String delete() throws Exception{
        JsonResult<String> json=dictService.openSessiondelete(ids);
        outJson(json);
        return null;
    }

    public Dict getDict()
    {
        return dict;
    }

    public void setDict(Dict dict)
    {
        this.dict = dict;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    @Resource
    @Required
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    /**
     * @return the dictService
     */
    public IDictService getDictService()
    {
        return dictService;
    }

    @Resource
    @Required
    public void setDictService(IDictService dictService)
    {
        this.dictService = dictService;
    }

    public List<Dict> getTypeList()
    {
        return typeList;
    }

    public void setTypeList(List<Dict> typeList)
    {
        this.typeList = typeList;
    }

    public Long[] getDicIds()
    {
        return dicIds;
    }

    public void setDicIds(Long[] dicIds)
    {
        this.dicIds = dicIds;
    }

    public Long[] getDicNums()
    {
        return dicNums;
    }

    public void setDicNums(Long[] dicNums)
    {
        this.dicNums = dicNums;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

}
